<?php
 include "counter.php";
 iamashowpage();
?>
<?php pagehead("ADM: Display person $user") ?>
<BODY bgcolor="white">
<?php pagetop() ?>
<?php
showmessage();

$user = $counter->get_input("user", "number", "get");
$u = new User($user);


?>

<h1><?php echo "$user ", $u->pfields[name] ?></h1>
<?php

showstate($u);
?>
<form method="get" action="/cgi-bin/adm/useractions.cgi">
<input type="hidden" name="user" value="<?php echo $user ?>">
Actions:
<?php if ($u->ufields[state] == "frozen") { ?>
<input type="submit" name="act.edit" value="Thaw & Edit">
<input type="submit" name="act.research" value="Research!">
<input type="submit" name="act.untraceable" value="Unfindable">
<input type="submit" name="act.nolinux" value="Not a Linux user">
<input type="submit" name="act.drop" value="I give up">
<?php } else { ?>
<input type="submit" name="act.edit" value="Edit">
<input type="submit" name="act.freeze" value="Freeze">
<?php } ?>
</form>
<?
showdates($u);
if ($u->hasperson()) {
   showperson($u);

   print "<h3>Geography</h3>\n";
   showplace($u);
}
print "<h3>Emails</h3>\n";
showemails($u);

if ($u->ufields[state] == "frozen") {
   showfrozen($u);
}

if ($u->ufields[login]) {
   showmaintainedplaces($u);
}


?>
<?php pagebottom() ?>
</body>
</html>


<?php
function showstate($u) {

	echo "State: <b>",  $u->ufields[state], "</b>";
	 if ($u->ufields[comment]) {
      		echo "<br> Comment: ", $u->ufields[comment];

	}

}

function showperson($u) {
?>
<h3>Person record</h3>
<table>
<tr><td>Name<td><?php echo $u->pfields[name] ?></tr>
<tr><td>Homepage<td><?php echo $u->pfields[homepage] ?></tr>
<tr><td>Started<td><?php echo $u->pfields[started] ?></tr>
<tr><td>Usage<td><?php echo $u->pfields[usage] ?></tr>
<tr><td>May_publish<td><?php echo $u->pfields[may_publis] ?></tr>
</table>
<form method=POST action="/cgi-bin/adm/charsetfix.cgi">
<input type="hidden" name="file" value="persons">
<input name="f_key" type="hidden" value="<?php print $u->userid ?>">
<input type="submit" name="to-utf8" value="Convert to UTF-8">
<input type="submit" name="from-utf8" value="Convert from UTF-8">
</form>
<?php
}

function showdates($u) {

?>
<table compact>
<tr><th colspan=2>Dates and times</th>
<tr><td>User created</td><td><?php echo $u->ufields[f_ctime] ?></td></tr>
<tr><td>User updated</td><td><?php echo $u->ufields[f_mtime]
?></td></tr>
<?php
if ($u->hasperson()) {
?>
<tr><td>Peson created</td><td><?php echo $u->pfields[f_ctime] ?></td></tr>
<tr><td>Person updated</td><td><?php echo $u->pfields[f_mtime]
?></td></tr>
<?
}
?>
<tr><td>Last login</td><td><?php echo $u->ufields[logintime] ?></td></tr>
<tr><td>Last email</td><td><?php echo $u->ufields[emailtime] ?></td></tr>
</table>
<?php
}

function showemails($u) {

    $key = $u->userid;
    $query = mysql_query("select f_key, email.usage, state, level, error from email where owner = $key");
    if ($query == 0) {
       print("MySQL error: " . mysql_errno() . ":" . mysql_error() . "<br>");
       die("Query for emails failed");
    }
    print("Contact: " . $u->ufields[email] . "<br>\n");
    if ($u->hasperson()) {
        print("Public: " . $u->pfields[email] . "\n");
    }
    print "<table border>\n";
    print "<tr><th>Use<th>Email<th>State<th>Level<th>Error msg</tr>\n";
    while ($arr = mysql_fetch_array($query)) {
	print "<tr><td>";
	print $arr[usage];
	print "<td>" . $arr[f_key];
	print "<td>" . $arr[state];
	if ($arr[state] <> "ok") {
	    print "<td>" . $arr[level];
	    print "<td>" . $arr[error];
        }
	print "\n";
    }
    print "</table>\n";
}

function showplace($u) {
   $placeid = $u->pcfields[placeid];

    $query = mysql_query("select f_key from places where name = '$placeid'");
    if (!$query) {
       print("MySQL error: " . mysql_errno() . ":" . mysql_error() . "<br>");
       die("Query for placekey from placeid failed");
    }
	
    $arr = mysql_fetch_row($query);
    $id = $arr[0];

   $pl = cleanfields("places", $id);
   $pname = $pl[longname];
   while ($pl[within] > 1) { # skip The World (1)
	$pname .= ", ";
	$pl = cleanfields("places", $pl[within]);
	$pname .= $pl[longname];
   }
   print "<table border>";
   print "<tr><td>Place<td>" . $pname . "<td>" . $placeid . "\n";
   print "<tr><th>Component<th>Entered<th>Interpreted";
   print "<tr><td>Country<td>" . $u->pfields[country]
	              . "<td>" . $u->pcfields[country] . "\n";
   print "<tr><td>State<td>" . $u->pfields[state]
	              . "<td>" . $u->pcfields[state] . "\n";
   print "<tr><td>City<td>" . $u->pfields[city]
	              . "<td>" . $u->pcfields[city] . "\n";
   print "</table>\n";
}

function showfrozen($u)
{
   print "<h3>Frozen records</h3>\n";
    $key = $u->userid;
    $query = mysql_query("select f_key from frozen where owner = $key");
    if ($query == 0) {
       print("MySQL error: " . mysql_errno() . ":" . mysql_error() . "<br>");
       die("Query for frozen records failed");
    }
    print "<table border>\n";
    print "<tr><th>Table<th>Record</tr>\n";
    while ($arr = mysql_fetch_array($query)) {
        $rec = rawfields("frozen", $arr[0]);
	print "<tr><td>";
	print $rec[ftable];
	$text = preg_replace("/\\\\n/", "<br>", $rec[textform]);
	print "<td>" . $text;
	print "\n";
    }
    print "</table>\n";

}
 
function showmaintainedplaces($u)
{
  print "<h3>Places he maintains</h3>\n";
  $key = $u->userid;
  $query = mysql_query("select f_key, name, longname from places, link_place_maintainer where places.f_key = link_place_maintainer.fromkey and link_place_maintainer.tokey = $key");
  if ($query == 0) {
    print("MySQL error: " . mysql_errno() . ":" . mysql_error() . "<br>");
    die("Query for maintained places failed");
  }
  print "<table border>\n";
  while ($arr = mysql_fetch_array($query)) {
    print "<tr><td>" . $arr[0] . "<td>" . $arr[1] . "<td>" . $arr[2] . "\n";
  }
  print "</table>\n";
}

?>
